/*
 * Licensed to the Apache Software Foundation (ASF) under one
 * or more contributor license agreements.  See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership.  The ASF licenses this file
 * to you under the Apache License, Version 2.0 (the
 * "License"); you may not use this file except in compliance
 * with the License.  You may obtain a copy of the License at
 *
 *  http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing,
 * software distributed under the License is distributed on an
 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 * KIND, either express or implied.  See the License for the
 * specific language governing permissions and limitations
 * under the License.
 */
package com.gr.staffpm.pages.behaviour;

import org.apache.wicket.Component;
import org.apache.wicket.behavior.AbstractBehavior;
import org.apache.wicket.markup.html.IHeaderContributor;
import org.apache.wicket.markup.html.IHeaderResponse;
import org.odlabs.wiquery.core.options.Options;

import com.gr.staffpm.widget.jquerywatermark.js.WatermarkJavaScriptResourceReference;

/**
 * @see http://code.google.com/p/jquery-watermark/
 * @author Graham Rhodes 3 Jan 2011 22:49:14
 */
public class TextFieldWatermarkBehaviour extends AbstractBehavior implements IHeaderContributor {

    private static final long serialVersionUID = 1L;
    private final Options options = new Options();
    private final String text;
    private Component component;

    public TextFieldWatermarkBehaviour(String text) {
        this.text = text;
    }

    /* (non-Javadoc)
     * @see org.apache.wicket.behavior.AbstractBehavior#bind(org.apache.wicket.Component)
     */
    @Override
    public void bind(org.apache.wicket.Component component) {
        super.bind(component);
        this.component = component;
    }

    /* (non-Javadoc)
     * @see org.apache.wicket.behavior.AbstractBehavior#renderHead(org.apache.wicket.markup.html.IHeaderResponse)
     */
    @Override
    public void renderHead(IHeaderResponse response) {
        super.renderHead(response);
        response.renderJavascriptReference(WatermarkJavaScriptResourceReference.get());
        String javascript = "(function($){$(document).ready(function(){$('#" + component.getMarkupId() + "').watermark('" + text + "',{});});})(jQuery);";
        response.renderJavascript(javascript, null);
    }

    /** Set's the className.
     * @param className The className to set
     * @return instance of the current behavior
     */
    public TextFieldWatermarkBehaviour setClassName(String className) {
        this.options.putLiteral("className", className);
        return this;
    }

    /**
     * @return The ClassName
     */
    public String getClassName() {
        return this.options.getLiteral("className");
    }

    /** Set's the useNative.
     * @param useNative The useNative to set
     * @return instance of the current behavior
     */
    public TextFieldWatermarkBehaviour setUseNative(boolean useNative) {
        this.options.put("useNative", useNative);
        return this;
    }

    /**
     * @return The useNative
     */
    public boolean isUseNative() {
        Boolean useNative = this.options.getBoolean("useNative");
        return (useNative != null) ? useNative : true;
    }
}
